﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameEngine.Entities;
using GameEngine.EntityComponents;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PvT.Entities
{
    public class AnimationEntity : AbstractDrawableEntity
    {
        private int singleImageWidth = 120;
        private int singleImageHeight = 120;

        private int animationFrameRate = 12;
        private int animationImages = 10;

        /// <summary>
        /// Initializes a new instance of the <see cref="AnimationEntity" /> class.
        /// </summary>
        /// <param name="components">The components.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        /// <param name="isVisible">if set to <c>true</c> [is visible].</param>
        /// <param name="position">The position.</param>
        /// <param name="game">The game.</param>
        public AnimationEntity(List<IEntityComponent> components, bool isActive, bool isVisible, Vector2 position, Game game) :
            base(components, isActive, isVisible, position, game)
        {
            this.scale = 3.0f;
        }

        /// <summary>
        /// Loads the content.
        /// </summary>
        public override void LoadContent()
        {
            base.texture = game.Content.Load<Texture2D>("Scenes/ExplosionSpriteSheet");
        }

        public override void Update(GameTime gameTime)
        {
        }

        /// <summary>
        /// Draws the AnimationEntity
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        /// <param name="gameTime">The game time.</param>
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            int runFrame = (int)(gameTime.TotalGameTime.TotalSeconds * animationFrameRate) % animationImages;
            sourceRectangle = new Rectangle((runFrame * singleImageWidth), 0, singleImageWidth, singleImageHeight);
            this.DrawWithoutCamera(spriteBatch, gameTime);
        }

        /// <summary>
        /// Draws the without camera.
        /// </summary>
        /// <param name="spriteBatch">The sprite batch.</param>
        /// <param name="gameTime">The game time.</param>
        private void DrawWithoutCamera(SpriteBatch spriteBatch, GameTime gameTime)
        {
            if (!this.isVisible)
                return;
            spriteBatch.Begin();

            if (sourceRectangle == Rectangle.Empty)
                spriteBatch.Draw(texture, position, null, Color.White, rotation, origin, scale, effects, layerDepth);
            else
                spriteBatch.Draw(texture, position, sourceRectangle, Color.White, rotation, origin, scale, effects, layerDepth);

            spriteBatch.End();
        }
    }
}
